package org.kfive.nova.hadoop;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/*
* Dedup.java
* Created  by  dell
* Created on 2016/3/30 14:19
*/
public class Dedup {

    public static class DedupMapper extends Mapper<LongWritable,Text,Text,NullWritable>{

        public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException{
            context.write(value, NullWritable.get());
        }
    }

    public static class DedupReducer extends Reducer<Text,NullWritable,Text,NullWritable>{

        public void reduce(Text key,Iterable<NullWritable> values,Context context)throws IOException,InterruptedException{
            context.write(key,NullWritable.get());
        }
    }


    public static void main(String[] args) throws Exception{

        if(args.length!=2){
            System.err.println("WordCount error : Illegal arguments");
            System.exit(-1);
        }

        Job job = new Job();
        job.setJarByClass(Dedup.class);
        job.setJobName("Dedup");

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.setMapperClass(DedupMapper.class);
        job.setReducerClass(DedupReducer.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
